﻿
$(document).ready(function(){
	/**
	*查询面板
	*/
	Chenxin.UI.HouseManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
		id: 'house_search',
		initEvent: function() {
			var _this = this;
			$('#btn_search').click(function() {
				_this.scope.reloadGrid();
			});

			$('.a-qucik-search').click(function() {
				var sm = _this.getParamJson();
				sm.quicktype = this.id;
				_this.scope.grid.reload(sm);
				$(".menu_list").hide(1, function() { $(this).parent(".tlBtn").removeClass("click") });
			});

			$("#resetbutton").live('click', function() {
				$("#daybegin").val("");
				$("#dayend").val("");
				_this.reset();
				_this.daytype.setValue('20');
				_this.priceType.setValue('t_price');
				
				$('#lowprice').defText({
					deftxt: '单位:万'
				})
				$('#heightprice').defText({
					deftxt: '单位:万'
				})
				
			});

			//小区自动填充
			$('#txtHousing').autocomplete({
				deferRequestBy: 5,
				width: 200,
				serviceUrl: this.scope.actionUrl + '?op=loadcommunity'
			});
			$('#txtHousing').defText({
				deftxt: '输入楼盘名称'
			});
			$('#txt_bname').defText({
				deftxt: '输入栋号'
			})
			$('#txt_room').defText({
				deftxt: '输入室'
			})
			$('#keyword').defText({
				deftxt: '输入地址/编号/楼盘'
			})
			$('#txt_usercode').defText({
				deftxt: '输入工号'
			})
			$('#lowprice').defText({
				deftxt: '单位:万'
			});
			$('#heightprice').defText({
				deftxt: '单位:万'
			});
			$('#lowprice').numberText({
				minValue:0
			});
			$('#heightprice').numberText({
				minValue:0
			});
			$('#floor_start').numberText({
				minValue:0
			});
			$('#floor_end').numberText({
				minValue:0
			});
			
			$('#area_start').numberText({
				minValue:0
			});
			$('#area_end').numberText({
				minValue:0
			});
			
			$('#customer_tel').defText({
				deftxt: '业主电话'
			});

		},
		createComboList: function() {
		
			var _wc=new Chenxin.UI.RegionBlockWinCombo({
				containerId:'div_region_block'
			});
			
			var status = new Chenxin.UI.Combo({
				containerId: '#status',
				tbxWidth: 45,
				addPleaseSelected:false,
				items: comboCommon.houseStatus1
			});
			var business = new Chenxin.UI.Combo({
				containerId: '#business',
				tbxWidth: 45,
				items: comboCommon.houseBusiness
			});
			var houseuse = new Chenxin.UI.ParameterCombo({
				containerId: '#houseuse',
				key: 'House.Use',
				autoLoad: true,
				tbxWidth: 45,
				inModelType: 'txt',
				paramType: 'text'
			});
			var buildingtype = new Chenxin.UI.ParameterCombo({
				containerId: '#buildingtype',
				key: 'Building.Type',
				autoLoad: true,
				tbxWidth: 90,
				inModelType: 'txt',
				paramType: 'text'
			});


			//装修程度
			var fitment = new Chenxin.UI.ParameterCombo({
				containerId: '#div_fitment',
				name: 'Fitment',
				key: 'House.Fitment',
				inModelType: 'txt',
				tbxWidth: 45
			});

			this.daytype = new Chenxin.UI.Combo({
				containerId: '#daytype',
				initValue: '20',
				tbxWidth: 45,
				colNum: 1,
				addPleaseSelected: false,
				items: comboCommon.dateType
			});

			var markStatus = new Chenxin.UI.Combo({
				containerId: '#marked',
				tbxWidth: 45,
				items: comboCommon.MarkStatus
			});

			var protectedStatus = new Chenxin.UI.Combo({
				containerId: '#protected',
				tbxWidth: 45,
				items: comboCommon.ProtectedStatus
			});


			this.priceType = new Chenxin.UI.Combo({
				containerId: '#price_type',
				tbxWidth: 45,
				colNum: 1,
				name: 'pricetype',
				initValue: 't_price',
				addPleaseSelected: false,
				items: [{ txt: '总价', val: 't_price' }, { txt: '单价', val: 'u_price'}],
				onSelected: function() {
					if (this.getValue() == 't_price') {
						$('#lowprice').defText({
							deftxt: '单位:万'
						})
						$('#heightprice').defText({
							deftxt: '单位:万'
						})
					} else { 
					  $('#lowprice').defText({
							deftxt: '单位:元'
						})
						$('#heightprice').defText({
							deftxt: '单位:元'
						})
					}
				}
			});


			var key = new Chenxin.UI.Combo({
				containerId: '#div_key',
				tbxWidth: 45,
				colNum: 1,
				name: 'haskey',
				items: [{ txt: '有', val: '10' }, { txt: '无', val: '0'}]
			});

			var hall = new Chenxin.UI.Combo({
				containerId: '#div_hall',
				tbxWidth: 24,
				colNum: 1,
				showMode:'multi',
				name: 'househall',
				defTxt: '厅',
				items: [{ txt: '1厅', val: '1' }, { txt: '2厅', val: '2' }, { txt: '3厅', val: '3' }, { txt: '4厅', val: '4' }, { txt: '5厅', val: '5'}]
			});

			var room = new Chenxin.UI.Combo({
				containerId: '#div_room',
				tbxWidth: 24,
				name: 'houseroom',
				showMode:'multi',
				colNum: 1,
				defTxt: '室',
				items: [{ txt: '1室', val: '1' }, { txt: '2室', val: '2' }, { txt: '3室', val: '3' }, { txt: '4室', val: '4' }, { txt: '5室', val: '5'}]
			});

			var combl = new Chenxin.UI.StructComboList({
				cityAfterSelected: function(val) {
					_wc.reload({cityid:val});
				}
			});
			var structs = combl.comboList;
			structs.push(_wc);
			structs.push(status);
			structs.push(business);
			structs.push(houseuse);
			structs.push(buildingtype);
			structs.push(this.daytype);
			structs.push(markStatus);
			structs.push(protectedStatus);
			structs.push(this.priceType);
			structs.push(key);
			structs.push(hall);
			structs.push(room);
			structs.push(fitment);

			return structs;
		},
		initComponent: function() {
			Chenxin.UI.HouseManageSearch.superclass.initComponent.call(this);
		}
	});
	var houseManage=new Chenxin.UI.HouseManage({
		createGrid: function() {
			var _this = this;
			return new Chenxin.UI.ExpandGrid({
				actionUrl: '/service/houseCensorNew.ashx?op=getlist',
				id: 'houselist',
				hasRowNumber: false,
				height:300,
				renderTo: 'house_table',
				hasCheckBox: false,
				autoLoad:false,
				afterRenderTitleTool: function() {
					var _obj = this;
					this.sortCombo = new Chenxin.UI.Combo({
						containerId: '#xyz',
						name: 'sortField',
						defVal: '',
						defTxt: '默认排序',
						colNum: 1,
						colWidth: 105,
						items: [
							{ txt: '按跟盘时间升序', val: 'servicetime asc', icon: 'x-sort-asc' },
							{ txt: '按跟盘时间降序', val: 'servicetime desc', icon: 'x-sort-desc' },
							{ txt: '按建档时间升序', val: 'adddate asc', icon: 'x-sort-asc' },
							{ txt: '按建档时间降序', val: 'adddate desc', icon: 'x-sort-desc' }
						],
						onSelected: function() {
							_obj.sortField = this.getValue();
							_obj.reload();
						},
						afterHeadSelect: function(el) {
							if (el.f) {
								this.sortCombo.setVal(this.sortDir.field.toLowerCase() + ' ' + this.sortDir.dir);
							}
						}
					});
				},
				createTitleTool: function() {
					return [{
						text: '<div style="float:left;">排序：</div><div class="SelectWrapSingle" id="xyz" style="z-index:1; margin-top:2px;"></xyz>',
						showTitle: false
						}];
				},
				afterHeadSelect: function(el) {
					var _sf = this.sortDir.field.toLowerCase() + ' ' + this.sortDir.dir;
					if (this.sortCombo.findTxtByVal(_sf)) {
						this.sortCombo.setValue(_sf);
					} else {
						this.sortCombo.reset();
					}
				},
				scope: this,
				rowExpandAble: true,
				rowExpand: function(dom, el, grid) {
					_this.loadHouseInfo();
				}
			});
        },
        renderHouseInfo: function() {
            var m = this.currentHouseInfo;
            var infoStr = '';
            infoStr += '<div class="houtab fl">';
            infoStr += '<ul class="padding5">';
            infoStr += '<li style="float:left;width:380px;"><b>' + m.Community;
            switch (m.BusinessKind) {
                case 10: //出售
                    var p = (m.SalePrice_T / 10000);

                    infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(0) + '万</span>';
                    break;
                case 20: //出租
                    infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                    break;
                case 30: //租售
                    var p = (m.SalePrice_T / 10000);

                    infoStr += '&nbsp;&nbsp;<span class="red20">售价：' + p.toFixed(0) + '万</span>';
                    infoStr += '&nbsp;&nbsp;<span class="red20">租价：' + m.RentPrice + '元</span>';
                    break;
            }

            infoStr += '</b></li>';
            infoStr += '<li style="float:left;width:360px;">地址：' + m.Address + ' ' + ' ' + m.BuildingName + ' ' + m.Room + '</li>';
            infoStr += '<li style="float:left;width:230px;">编号：' + m.HouseCode + '</li>';
            if (m.BusinessKind != 20) {
                infoStr += '<li style="float:left;width:110px;">单价：' + (m.SalePrice_T / m.BuildingArea).toFixed(0) + '元</li>';
            }
            infoStr += '<li style="float:left;width:230px;">板块：' + m.Block + '</li>';
            infoStr += '<li style="float:left;width:100px;">类型：' + m.BuildingType + '</li>';
            infoStr += '<li style="float:left;width:230px;">建档日期：' + m.Adddate + '</li>';
            infoStr += '<li style="float:left;width:100px;">装修：' + m.Fitment + '</li>';
            infoStr += '<li style="float:left;width:380px;">';
            infoStr += '<div class="btn_pon_3"><a href="ShowDetailInfo.aspx?houseid=' + m.HouseId + '" target="_blank" title="查看详情" >详情</a></div>'
            infoStr += '<input name="btn_viewownerinfo" type="button" class="btn_pon" value="查看业主"/>';
            infoStr += '<input name="btn_trace" type="button" customerid='+m.CustomerId+' class="btn_pon" value="追踪"/>';
            infoStr += '<input name="btn_change" type="button" class="btn_pon" value="变更"/>';
			infoStr += '<input name="btn_del" type="button" class="btn_pon" value="删除"/>';
            infoStr += '</li>';
            infoStr += '</ul>';
            infoStr += '</div>';
            infoStr += '<div class="gdtab"><div>';
            infoStr += '<div id="housetrace_table" style="height:123px;"></div>';
            infoStr += '</div></div>';
            $('#exp_' + this.currentId).find('dd').append(infoStr);
        },
		createTraceList: function(count) {
            if (!this.tracePage) {
                this.tracePage = new Chenxin.UI.Pagination({
                    id: 'housetrace',
                    renderTo: 'housetrace_table',
                    pageSize: 4,
                    actionUrl:'/service/houseCensorNew.ashx?op=gethousetracelist',
                    data: { houseid: this.currentId }
                });
            } else {
                this.tracePage.recordCount = 0;
                this.tracePage.hasCount = false;
                this.tracePage.reload({ houseid: this.currentId });
            }
        },
		initEvent: function() {
            var _this = this;
            //查询菜单点击展开事件
            $(".arrow_1").click(function() {
                $("#panel").slideToggle("slow");
                $(this).toggleClass("up_1");
                return false;
            });
			$('.export').live('click', function() {
				if ($("#isExport").val() == 'can') {
					Chenxin.ajax({
						type: "post",
						dataType: "text",
						paramType: 'json',
						url: 'service/houseCensorNew.ashx',
						data: { op: 'export' },
						onSuccess: function(data) {
							_this.exportData();
						}
					});
				} else {
					alert('请勾选可导出选项，查询后再执行本操作！');
				}
			});
            $("input[name='btn_viewownerinfo']").live('click', function() {
                _this.showCustomerInfo();
            });
            $("input[name='btn_trace']").live('click', function() {
                _this.showDetails('getcustomerlist',_this.currentHouseInfo.CustomerId,_this.currentHouseInfo.HouseId);
            });

            $("input[name='btn_change']").live('click', function() {
				_this.showChangeConfirm();
            });
			$("input[name='btn_del']").live('click', function() {
			    if(confirm("是否删除该记录！")){
                    _this.deleteHouse();
                }
            });
        },        
		deleteHouse: function() {
            if (this.currentId) {
                Chenxin.ajax({
                    type: "get",
                    url: "service/houseCensorNew.ashx",
                    data: {
						op:'del',
                        modelid: this.currentId
                    },
                    dataType: "text",
                    onSuccess: function(data, msg) {
                        alert(msg);
                    }
                });

            } else {
                alert('请先选择一条记录！');
            }
        },
		createSearch: function() {
            return new Chenxin.UI.HouseManageSearch({
                scope: this
            });
        },
        showDetails: function(op,customerid,houseid) {
            if (!this.detailsWin) {
                this.detailsWin = new Chenxin.UI.DetailsWin({
                    id: 'x_details_win',
                    actionUrl: 'service/houseCensorNew.ashx'
                });
            }
            this.detailsWin.show();

            this.detailsWin.setTitle('客户记录变动明细');
            this.detailsWin.grid.baseParam.op = op;
            this.detailsWin.grid.param.customerid = customerid;
            this.detailsWin.grid.param.houseid = houseid;
            this.detailsWin.grid.reload();
        },
		showChangeConfirm: function() {
			var _this = this;
			if (!this.currentId) {
				alert('请先选择一条房源记录！');
				return;
			}
			if (!this.changeConfirmWin) {
				this.changeConfirmWin = new Chenxin.UI.ChangeForm({
					currentId: this.currentId,
					scope: this,
					afterGetFormJson: function(json) {
						this.changeConfirmWin.op = "Edit";
						delete json.AddPerson
						delete json.AddDepart
						delete json.Address;
						delete json.nowStatus;
						if (this.changeConfirmWin.requestUser) {
							json.RequestUserId = this.changeConfirmWin.requestUser.UserId;
							json.RequestDepartId = this.changeConfirmWin.requestUser.DepartId;
							json.RequestUserCode = this.changeConfirmWin.requestUser.UserCode;
						}
						if (json.ChangeType == '10') {//状态变更，无需传递此参数
							if (json.RequestDepartId) {
								delete json.RequestDepartId;
							}
							if (json.RequestUserId) {
								delete json.RequestUserId;
							}
							if (json.RequestDepart) {
								delete json.RequestDepart;
							}
							if (json.RequestUser) {
								delete json.RequestUser;
							}
							if (json.RequestUserCode) {
								delete json.RequestUserCode;
							}
						} else if (json.ChangeType == '20') {//人员变更，无需传递此参数	
							if (json.HouseStatus) {
								delete json.HouseStatus;
							}
						}
						json.Mobile = json.request_customerMoblie;
						json.Phone = json.request_customerPhone;
						json.CustomerName = json.request_customerName;
						delete json.request_customerName
						delete json.request_customerPhone
						delete json.request_customerMoblie

						json.HouseId = _this.currentId;
					}
				});
			}

			this.changeConfirmWin.setTitle('房源变更');
			var _data = {};
			Chenxin.apply(_data, this.currentHouseInfo);
			_data.AddPerson = this.currentHouseInfo.OprPerson;
			_data.AddDepart = this.currentHouseInfo.OprDepart;
			_data.HouseStatus = _data.Status;
			_data.Remarks = '';
			var temp = this.currentHouseInfo.Status;
			if (temp == 10)
				temp = '有效';
			else if (temp == 20)
				temp = '暂缓';
			else if (temp == -1)
				temp = '无效';
			else if (temp == 30)
				temp = '封盘';
			else
				temp = '';
			_data.nowStatus = temp;
			this.changeConfirmWin.fitData(_data);
			$("#request_tr1").show();
			$("#request_tr2").hide();
			$("#request_tr3").hide();
			$("#request_tr4").hide();
			$("#request_tr5").hide();
			$("#request_tr6").hide();
			$("#request_tr7").hide();
			this.changeConfirmWin.show();
			this.changeConfirmWin.currentId = this.currentId;
		}
	});
	houseManage.reloadGrid();
});


Chenxin.UI.DetailsWin = Chenxin.extend(Chenxin.UI.Win, {
    id: '',
    actionUrl: '',
    width: 680,
    hasFoot: true,
    height: 360,
    content: '<div id="x_details_grid"></div>',
    createBtn: function() {
        return [{
            text: '关闭',
            handler: this.hide
}]
        },
        createGrid: function() {
            if (this.grid) {
                return;
            }
            this.grid = new Chenxin.UI.JsonGrid({
                baseParam: { op: '' },
                autoLoad: false,
                actionUrl: this.actionUrl,
                id: 'x_details_grid',
                renderTo: 'x_details_grid',
                hasCheckBox: false,
                cellBorder: true,
                modelId: 'a',
                scope: this,
                height: 311,
                hasTitle: false,
                columns: {
                    Operator: { head: '操作人', width: 80 },
                    OprType: { head: '客户类型', width: 100,
                        renderer: function(val, field, row) {
                            if (val == 0) {
                                return '基本信息';
                            }
                            else if (val == 100) {
                                return '需求信息';
                            }
                        }
                    },
                    Adddate: { head: '操作时间', width: 100,
                            renderer: function(val, field, row) {
                                if (val.length > 0) {
                                    return val.split(' ')[0];
                                }
                                else {
                                    return '';
                                }
                            }
                        },
                    ChangeDesc: { head: '变动明细', width: 350 ,style: 'text-align:left',}                    
                }
            });
        },
        initComponent: function() {
            Chenxin.UI.DetailsWin.superclass.initComponent.call(this);
            this.createGrid();
        }
    });